﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

using System.Threading;

using System.IO;

namespace _1vs100
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class Window1 : Window
    {
        Label[] mobLabel;
        Label[] levelLabel;
        QuestionWindow questionWindow;
        MediaPlayer openingPlayer, crowdPlayer;
        //List<MobMember> eliminatedMobMembers = new List<MobMember>();
        int questionsAsked;

        public Window1()
        {
            mobLabel = new Label[100];
            levelLabel = new Label[10];
            questionWindow = new QuestionWindow();
            openingPlayer = new MediaPlayer();
            crowdPlayer = new MediaPlayer();
            questionsAsked = 0;
            InitializeComponent();
            setProperties();
        }

        /****************************************************Private Methods */

        private void setProperties()
        {
            setMobLabels();
            setLevelLabels();
            this.ResizeMode = ResizeMode.NoResize;
            this.ResizeMode = ResizeMode.CanMinimize;
            this.Closing += new System.ComponentModel.CancelEventHandler(Window1_Closing);
            this.WindowStartupLocation = WindowStartupLocation.CenterScreen;
            btnStartGame.IsEnabled = false;
            btnNextQuestion.IsEnabled = false;
            txtPlayer.Text = ((App)Application.Current).currentGame.currentPlayer.Name;
            txtPotAmt.Text = ((App)Application.Current).currentGame.currentPot.ToString();

            questionWindow.IsVisibleChanged += new DependencyPropertyChangedEventHandler(questionWindow_IsVisibleChanged);
            questionWindow.Closed += new EventHandler(questionWindow_Closed);
            this.Loaded += new RoutedEventHandler(Window1_Loaded);
        }

        private void guiUpdate()
        {
            int i = 0, mobEliminated = 0;

            for (i = 0; i < ((App)Application.Current).currentGame.CurrentMob.Count; i++)
            {
                if (!(((App)Application.Current).currentGame.CurrentMob.ElementAt(i).Value.Answer.Equals(((App)Application.Current).currentGame.currentAnswer))
                    && !(((App)Application.Current).currentGame.CurrentMob.ElementAt(i).Value.Eliminated))
                {
                    mobLabel[((App)Application.Current).currentGame.CurrentMob.ElementAt(i).Key].Background = new SolidColorBrush(Colors.Red);
                    ((App)Application.Current).currentGame.CurrentMob.ElementAt(i).Value.Eliminated = true;
                    mobEliminated++;
                }
            }

            //Need to come up with method not to loop over the ones eliminated

            //Thread.Sleep(1000);
            playCrowd();
            lblMainContent.Content = mobEliminated.ToString() + " mob members have been eliminated!";
            ((App)Application.Current).currentGame.mobEliminated += mobEliminated;
            updatePot(mobEliminated);
        }

        private void checkIfWon()
        {
            if (((App)Application.Current).currentGame.mobEliminated == 100 || (((App)Application.Current).currentGame.currentLevel == 10))
            {
                btnNextQuestion.IsEnabled = false;
                if (((App)Application.Current).currentGame.mobEliminated == 100)
                {
                    MessageBox.Show("Congratulations!! You have knocked out all 100 mob members", "You Win!");
                }
                else
                {
                    MessageBox.Show("Congratulations!! You have answered all the questions", "You Win!");
                }
                lblMainContent.Content = "Congratulations " + txtPlayer.Text + "! You have won " + txtPotAmt.Text + " dollars!";
                btnExitGame.Focus();
            }
        }

        private void updatePot(int mobElim)
        {
            int pot = 0;

            if (((App)Application.Current).currentGame.currentLevel == 1)
            {
                pot = mobElim * 1000;
            }
            else if (((App)Application.Current).currentGame.currentLevel == 2)
            {
                pot = mobElim * 2000;
            }
            else
            {
                pot = mobElim * ((App)Application.Current).currentGame.levels[((App)Application.Current).currentGame.currentLevel - 1];
            }
            lblMainContent.Content += "\n You won " + pot + " dollars!";
            ((App)Application.Current).currentGame.currentPot += pot;
            txtPotAmt.Text = ((App)Application.Current).currentGame.currentPot.ToString();
        }

        private void btnNextQuestion_Click(object sender, RoutedEventArgs e)
        {
            lblMainContent.Content = "Let's play 1 vs " + (100 - ((App)Application.Current).currentGame.mobEliminated).ToString() + "!";

            questionsAsked++;
            if (questionsAsked == 3 && ((App)Application.Current).currentGame.currentLevel == 1)
            {
                questionsAsked = 0;
                increaseLevel();
            }
            else if (questionsAsked == 2 && ((App)Application.Current).currentGame.currentLevel == 2)
            {
                questionsAsked = 0;
                increaseLevel();
            }
            else if (((App)Application.Current).currentGame.currentLevel > 2)
            {
                increaseLevel();
            }
            questionWindow.showNextQuestion();
            questionWindow.enableLifeLine();
            questionWindow.Visibility = Visibility.Visible;
            questionWindow.playQuestionMusic();
        }

        private void increaseLevel()
        {
            ((App)Application.Current).currentGame.currentLevel++;
            levelLabel[((App)Application.Current).currentGame.currentLevel - 1].Background = Brushes.LawnGreen;
            levelLabel[((App)Application.Current).currentGame.currentLevel - 1].Foreground = Brushes.Yellow;

            levelLabel[((App)Application.Current).currentGame.currentLevel - 2].Background = Brushes.DarkViolet;
            levelLabel[((App)Application.Current).currentGame.currentLevel - 2].Foreground = Brushes.LightPink;
        }

        private void playCrowd()
        {
            crowdPlayer.Open(new Uri("crowd.mp3", UriKind.Relative));
            crowdPlayer.Play();
            crowdPlayer.MediaEnded += new EventHandler(crowdPlayer_MediaEnded);
        }

        private void setLevelLabels()
        {
            int i = 1, amt = 1000, bottom = 10;
            for (i = 1; i <= 10; i++)
            {
                Label tempLabel = new Label();
                tempLabel.Name = "lblLevel" + i.ToString();
                tempLabel.Height = 28;
                tempLabel.HorizontalContentAlignment = HorizontalAlignment.Center;
                tempLabel.VerticalAlignment = VerticalAlignment.Bottom;
                tempLabel.Background = Brushes.DarkViolet;
                tempLabel.Foreground = Brushes.LightPink;
                tempLabel.Content = "$  " + amt.ToString();
                tempLabel.FontSize = 16;
                tempLabel.Margin = new Thickness(40, 0, 40, bottom);
                tempLabel.Width = 140;
                bottom += 40;
                amt += 1000;

                levelLabel[i - 1] = tempLabel;
                grdLevel.Children.Add(tempLabel);
            }
        }

        private void setMobLabels()
        {
            int i = 1, left = 0, top = 0;
            for (i = 1; i <= 100; i++)
            {
                Label tempLabel = new Label();
                tempLabel.Name = "lblMob" + i.ToString();
                tempLabel.Height = 29;
                tempLabel.HorizontalAlignment = HorizontalAlignment.Left;
                tempLabel.VerticalAlignment = VerticalAlignment.Top;
                tempLabel.Background = Brushes.SkyBlue;
                tempLabel.Content = "Mob " + i.ToString();

                if (i == 100)
                {
                    tempLabel.Width = 60;
                }
                else
                {
                    tempLabel.Width = 53;
                }

                tempLabel.Margin = new Thickness(left, top, 0, 0);
                left += 60;
                if (i % 15 == 0)
                {
                    top += 50;
                    left = 0;
                }
                mobLabel[i - 1] = tempLabel;
                grdMainWindow.Children.Add(tempLabel);
            }

        }

        /****************************************************Event Handling Methods */

        void Window1_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            Application.Current.Shutdown();
        }

        void questionWindow_Closed(object sender, EventArgs e)
        {
            if (questionWindow.selfClose)
            {
                Application.Current.Shutdown();
            }
            else
            {
                btnNextQuestion.IsEnabled = false;
                lblMainContent.Content = "Sorry, you have lost.\n" + (100 - ((App)Application.Current).currentGame.mobEliminated).ToString() + " mob members will split the ";
                lblMainContent.Content += "winnings of " + ((App)Application.Current).currentGame.currentPot.ToString() + " dollars.";
                lblMainContent.Content += "\nThanks for playing 1 vs 100 created by Chao";
            }
        }

        void Window1_Loaded(object sender, RoutedEventArgs e)
        {
            openingPlayer.Open(new Uri("1vs100_Theme.mp3", UriKind.Relative));
            openingPlayer.Play();
            openingPlayer.MediaEnded += new EventHandler(openingPlayer_MediaEnded);
        }

        void openingPlayer_MediaEnded(object sender, EventArgs e)
        {
            btnStartGame.IsEnabled = true;
            btnStartGame.Focus();
        }

        void questionWindow_IsVisibleChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            if (questionWindow.Visibility == Visibility.Hidden)
            {
                //lblMainContent.Content = "Lets see how many mob members got it wrong!";
                btnNextQuestion.IsEnabled = false;
                if (!questionWindow.lifeLineSet)
                {
                    questionWindow.generateMobAnswers();
                }
                guiUpdate();
                checkIfWon();
                questionWindow.lifeLineSet = false;
            }
            else
            {
                if (((App)Application.Current).currentGame.timer)
                {
                    questionWindow.createProgressBar();
                }
            }
        }

        private void btnStartGame_Click(object sender, RoutedEventArgs e)
        {
            btnStartGame.IsEnabled = false;
            questionWindow.selfClose = true;

            levelLabel[0].Background = Brushes.LawnGreen;
            levelLabel[0].Foreground = Brushes.Yellow;
            //Thread.CurrentThread.Suspend

            questionWindow.showNextQuestion();
            questionWindow.playQuestionMusic();
            questionWindow.ShowDialog();
        }

        private void btnExitGame_Click(object sender, RoutedEventArgs e)
        {
            Application.Current.Shutdown();
        }

        void crowdPlayer_MediaEnded(object sender, EventArgs e)
        {
            if (((App)Application.Current).currentGame.currentLevel != 10)
            {
                btnNextQuestion.IsEnabled = true;
                btnNextQuestion.Focus();
            }
        }
    }
}
